<?php

// Last Modified: $Id: db_fns.php 216 2005-10-01 19:43:25Z plummer $

function db_connect()
{
	global $conf;
	$dbhost = $conf['db_server'];
	$result = pg_connect("host='$dbhost' user='uny-con' password='bob666' dbname='uny-con'"); 
	if (!$result)
	  return false;

   return $result;
}

function sql_last_inserted_id($result, $table_name, $column_name) { 
	$oid = pg_last_oid ($result);
		$query_for_id = "SELECT $column_name FROM $table_name WHERE oid=$oid"; 
	if (!($conn = db_connect()))
		return false;
	$result_for_id = pg_query($query_for_id); 
	if(pg_num_rows($result_for_id)) 
		$id=pg_fetch_array($result_for_id,0,PGSQL_ASSOC); 
	return $id[$column_name]; 
}


function db_result_to_array($result) {
	//untested
	$res_array = array();
	for ($count = 0; $row = @pg_fetch_array($result); $count++)
		$res_array[$count] = $row;
	return $res_array;
}


function table_create($result) 
{
   $numrows = pg_num_rows($result); 
   $fnum = pg_num_fields($result); 

   echo "<table border width='100%'>"; 
   echo "<tr>"; 

   for ($x = 0; $x < $fnum; $x++) { 
       echo "<td><b>"; 
       echo strtoupper(pg_field_name($result, $x)); 
       echo "</b></td>"; 
   } 

   echo "</tr>"; 

   for ($i = 0; $i < $numrows; $i++) { 
       $row = pg_fetch_object($result, $i); 
       echo "<tr align='center'>"; 
       for ($x = 0; $x < $fnum; $x++) { 
$fieldname = pg_field_name($result, $x); 
echo "<td>"; 
echo $row->$fieldname; 
echo "</td>"; 
       } 
       echo"</tr>"; 
   } 
   echo "</table>"; 

   return 0; 
}

function insert_badge_type($values) {
	//Inserts a new badge into the badge table.
	if (!db_connect())
		return false;
	extract($values);
	$attendee_id = $_SESSION['attendee_id'];
	$query = "INSERT INTO con_cost (badge_type, cost, user_available, description, modified, created,  modified_by) VALUES ('$badge_type', '$cost', '$user_available', '$description', 'now()', 'now()', '$attendee_id')";
	if (pg_query($query))  {
		return true; 
		} else {
		echo 'There was an error inserting the badge type into the database.';
		return false;
		}
}

function update_badge_type($values) {
	//Updates a badge's values in the badge table.
	if (!db_connect())
		return false;
	extract($values);
	$attendee_id = $_SESSION['attendee_id'];
	$query = "UPDATE con_cost SET badge_type = '$badge_type', cost = '$cost', description = '$description', user_available = '$user_available', modified = 'now()', modified_by = '$attendee_id' WHERE badge_type_id = '$badge_type_id'";
	if (pg_query($query))  {
		return true; 
		} else {
		echo 'There was an error updating the badge type in the database.';
		return false;
		}
}

function get_badge_types($badge_type_id ="%") {
	// Gets all badges in the con cost table and returns them as a postgres result resource.
	$conn = db_connect();
	if (!$conn)
		return false;
	$query = "SELECT * FROM con_cost WHERE badge_type_id ~~* '$badge_type_id' ORDER BY badge_type";
	$result = pg_query("$query");
	return $result;
}

function get_coupon_codes()
{
	$conn = db_connect();
	if (!$conn)
		return false;
	$query = "SELECT coupon_code FROM con_discount";
	$result = pg_query($query);
	return($result);
}

function insert_discount_info($values)
{
 $conn = db_connect();
  if (!$conn)
    return false;
	extract($values);
	$submit_date = date("Y/m/d 00:00:00-05");
	$query = "INSERT INTO con_discount (description, discount_amount, kickback_amount, num_of_uses, coupon_code, submit_date, expire_date) VALUES ('$description', '$discount_amount', '$kickback_amount', '$num_of_uses', '$coupon_code', '$submit_date', '$expire_date')";
	$result = pg_query($query);
}


?>
